package com.forest.oss;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.forest.oss.constants.AclType;

import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:
 * @Description
 * @date
 * @Version 1.0
 */
public abstract class AbstractOssClient implements IOssClient{

    private static Map<String, String> ossContentType = new HashMap<>();

    private static final String UNDEFINED_CONTENT_TYPE = "application/octet-stream";

    static {
        ossContentType.put("bmp", "application/x-bmp");
        ossContentType.put("exe", "application/x-msdownload");
        ossContentType.put("gif", "image/gif");
        ossContentType.put("htm", "ext/html");
        ossContentType.put("html", "ext/html");
        ossContentType.put("ico", "image/x-icon");
        ossContentType.put("jpe", "image/jpeg");
        ossContentType.put("jpeg", "image/jpeg");
        ossContentType.put("jpg", "image/jpeg");
        ossContentType.put("jsp", "text/html");
        ossContentType.put("js", "application/x-javascript");
        ossContentType.put("m2v", "video/x-mpeg");
        ossContentType.put("m3u", "audio/mpegurl");
        ossContentType.put("mp3", "audio/mp3");
        ossContentType.put("mp4", "video/mp4");
        ossContentType.put("mpeg", "video/mpg");
        ossContentType.put("mpv", "video/mpg");
        ossContentType.put("png", "application/x-png");
        ossContentType.put("pot", "application/vnd.ms-powerpoint");
        ossContentType.put("ppt", "application/vnd.ms-powerpoint");
        ossContentType.put("rmvb", "application/vnd.rn-realmedia-vbr");
        ossContentType.put("apk", "application/vnd.android.package-archive");
        ossContentType.put("doc", "application/msword");
        ossContentType.put("docx", "application/msword");
        ossContentType.put("xml", "text/xml");
        ossContentType.put("txt", "text/plain");
        ossContentType.put("xls", "application/vnd.ms-excel");
        ossContentType.put("xlsx", "application/vnd.ms-excel");
        ossContentType.put("pdf", "application/pdf");
    }

    /**
     * 获取文件类型
     */
    public  String getContentType(String filename) {
        String str = filename.substring(filename.lastIndexOf("."));
        if (str.startsWith(".")) {
            str = str.replace(".", "");
        }
        str = str.toLowerCase();
        return ossContentType.getOrDefault(str, UNDEFINED_CONTENT_TYPE);
    }

    @Override
    public String getFolder() {
        return getFolder("");
    }

    @Override
    public String getFolder(String project) {
        if (StrUtil.isNotBlank(project) && !project.endsWith("/")) {
            project = project.concat("/");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM");
        return project + simpleDateFormat.format(new Date(System.currentTimeMillis())) + "/";
    }

    @Override
    public String upload(String key, InputStream inputStream) {
        return upload(key,getFolder(),inputStream, AclType.PublicRead);
    }

    @Override
    public String upload(String key, InputStream inputStream, AclType acl) {
        return upload(key, getFolder(), inputStream, acl);
    }

    @Override
    public String upload(String key, String folder, InputStream inputStream) {
        return upload(key, folder, inputStream, AclType.PublicRead);
    }

    @Override
    public String upload(String key, String folder, File file) {
        return upload(key,folder,FileUtil.getInputStream(file));
    }

    @Override
    public String getFile(String key) {
        return getFile(key, AclType.PublicRead);
    }

    /**
     * 构建对象公共访问url.
     *
     * @param host
     * @param key
     * @return
     */
    protected String buildPublicUrl(String host, String key){
        return host.endsWith("/") ? key.startsWith("/") ? host + key.substring(1) : host + key : key.startsWith("/") ? host + key : host + "/" + key;
    }


    /**
     * byte字节大小 转换成 GB / MB /KB 单位
     */
    public  String formatSize(long size) {
        final int kb = 1024;
        if (size <= kb * kb) {
            return String.format("%.2f", (double) size / kb) + "KB";
        }
        if (size <= kb * kb * kb) {
            return String.format("%.2f", (double) size / (kb * kb)) + "MB";
        }
        return String.format("%.2f", (double) size / (kb * kb * kb)) + "GB";
    }
}
